<template>
  <li class="recItem" @click="todetail">
    <div class="top">
      <div class="image">
        <img :src="item.r.a.p" />
      </div>
      <h3>{{ item.r.a.n }}</h3>
      <i>LV.{{ item.r.a.lvl }}</i>
    </div>
    <div
      class="indexImage"
      :style="{
        height: (item.r.ph / item.r.pw) * 100 + 'vw',
      }"
    >
      <img :src="item.r.gif || item.r.p" alt="" />
    </div>
    <div class="fav">
      <div class="followuser">
        <span
          v-for="user in item.r.collect_users"
          :key="user.id * Math.random()"
          :style="{ backgroundImage: 'url(' + user.p + ')' }"
        >
        </span>
      </div>
      <div class="favNum">
        {{ item.r.collect_count_text }}
      </div>
      <div
        class="favbtn"
        :class="{ faved: faved }"
        @click.stop="toggetfav"
      ></div>
    </div>
    <div class="text">
      <h1>{{ item.r.n }}</h1>
    </div>
  </li>
</template>

<script>
import { mapState } from "vuex";
export default {
  props: ["item"],
  data: function () {
    return {
      faved: false,
    };
  },
  created: function () {
    // console.log(this.$store.state.favList);
    this.$store.state.favList.indexOf(this.item.r.id) != -1
      ? (this.faved = true)
      : (this.faved = false);
  },
  computed: {
    ...mapState(["favList"]),
  },
  watch: {
    favList(n) {
      n.indexOf(this.item.r.id) != -1
        ? (this.faved = true)
        : (this.faved = false);
    },
  },
  methods: {
    todetail() {
      this.$router.push({ path: `/detail?id=${this.item.r.id}` });
    },
    toggetfav() {
      if (localStorage.getItem("login") == "true") {
        if (this.faved) {
          this.$store.commit("cutfav", this.item.r.id);
        } else {
          this.$store.commit("addfav", this.item.r.id);
        }
        this.faved = !this.faved;
      } else {
        this.$router.push({ path: "/login?f=home&b=home" });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.recItem {
  // display: flex;
  .top {
    display: flex;
    align-items: center;
    padding: 10px;
    .image {
      width: 8vw;
      border-radius: 50%;
      overflow: hidden;
      img {
        vertical-align: middle;
      }
    }
    h3 {
      font-size: 13px;
      margin-left: 10px;
    }
    i {
      font-size: 12px;
      color: goldenrod;
      margin-left: 10px;
      line-height: 30px;
    }
  }
  .indexImage {
    background-color: #e6e6e6;
    overflow: hidden;
    img {
      vertical-align: middle;
    }
  }
  .fav {
    padding: 20px 10px;
    display: flex;
    align-items: center;
    .followuser {
      display: flex;
      span {
        display: block;
        width: 6vw;
        height: 6vw;
        border-radius: 50%;
        overflow: hidden;
        margin-left: -5px;
        border: 1px solid #fff;
        background-size: 100%;
        background-position: center;
        &:first-child {
          margin-left: 0;
        }
        img {
          vertical-align: top;
        }
      }
    }
    .favNum {
      flex: 1;
      font-size: 12px;
      color: #555;
      margin-left: 10px;
    }
    .favbtn {
      width: 6vw;
      height: 6vw;
      background-image: url("../assets/fav.png");
      background-size: 100%;
      background-position: center;
      &.faved {
        background-image: url("../assets/faved.png");
      }
    }
  }
  .text {
    padding: 0 13px 20px 10px;
    h1 {
      font-size: 13px;
      display: -webkit-box;
      overflow: hidden;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: (2);
    }
  }
}
</style>